package com.ruoyi.utils;

import cn.hutool.core.util.RandomUtil;
import com.alibaba.fastjson.JSONObject;
import com.alipay.api.AlipayApiException;
import com.alipay.api.AlipayClient;
import com.alipay.api.DefaultAlipayClient;
import com.alipay.api.request.AlipayTradePagePayRequest;
import com.alipay.api.response.AlipayTradePagePayResponse;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/**
 * @Author: 冰冰
 * @Date: 2023-02-16-11:00:46
 * @Description: 类
 */

public class AlipayConfig {

    private static String appid = "2021000122618221";

    private static String url = "https://openapi.alipaydev.com/gateway.do";

    private static String private_key ="MIIEwAIBADANBgkqhkiG9w0BAQEFAASCBKowggSmAgEAAoIBAQDWfgJ40jV+jI4g53hZ+S7TgmIQm1mHISHKnCU9Y0Bn22RK4f791hAVh75AaZJW2DenggvkDByiWlbCGB88la0P6xEzHLCY7sjaafdQzRACJvtwLKtty0cVY1BBH7YYJk426UOLGVBzBWDPqRCv0XdEl4MbApw5HOBVj8XI0UZY9IxecV2ED7xDqx9CkVL0ncHMuKULorTCg06cxMs6hVJEgFIZlSJ7E1lIRxF+k7ZOKWuJzeCaO1rzT2TNavAmKSbJWVlRrb+YWLPF4hEIRktlqYYpev4mezFhg8roeIqOD/WTtgBz0LprEbVagoFAZUbiUcxthuoVflp6zPr/erOlAgMBAAECggEBALjwPztjrCIoGKgATIHvW1OdOOauu7vKcnNavLU4baDexpPWVmERAB9AQmcRX88ncJ0qgQwo8Ut+7w09dTlsqKJwAQ178Dx8rou8EDUICoSAjg2DRnBNdEXZsqnkKMKbzJaF++nkcrwp5/ZFBJFD3z0yEiXo00BfSIIdi+CBqNEqvgGy8zs0o3RhXJmF8snjAF+OeCCVUXe+W0Mt4QxsrRJebbH4+T0y/gjaRvOlQgG7u+/GLirC1GVVC8IfdS717+7vB74TqnytrtgAH9xl27zWyd56AcDE0BMHz7oYf84E77aLeXASTH2zEYW361fwfRtdgU46MYhWY32PyptSMuECgYEA6tCiVlvEPMzk/pCH8T+26y/AHICJGKELr3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FGWPSMXnFdhA+8Q6sfQpFS9J3BzLilC6K0woNOnMTLOoVSRIBSGZUiexNZSEcRfpO2Tilric3gmjta809kzWrwJikmyVlZUa2/mFizxeIRCEZLZamGKXr+JnsxYYPK6HiKjg/1k7YAc9C6axG1WoKBQGVG4lHMbYbqFX5aesz6/3qzpQIDAQAB";




    public static void doPay(HttpServletResponse servletResponse, String successUrl, double price, String name){
        AlipayClient alipayClient = new DefaultAlipayClient(url,
                appid,private_key,"json","UTF-18",alipay_public_key,"RSA2");
        AlipayTradePagePayRequest request = new AlipayTradePagePayRequest();
        request.setNotifyUrl("http://www.zhuay.com/apliay/aliNotify");
        request.setReturnUrl(successUrl);
        JSONObject bizContent = new JSONObject();
        bizContent.put("out_trade_no", RandomUtil.randomNumbers(10));
        bizContent.put("total_amount", price);
        bizContent.put("subject",name);
        bizContent.put("product_code", "FAST_INSTANT_TRADE_PAY");


        request.setBizContent(bizContent.toString());
        AlipayTradePagePayResponse response = null;
        try {
            response = alipayClient.pageExecute(request);
        } catch (AlipayApiException e) {
            throw new RuntimeException(e);
        }
        if(response.isSuccess()){
            String body = response.getBody();
            String form= "" ;
            try  {
                form = alipayClient.pageExecute(request).getBody();  //调用SDK生成表单
            }  catch  (AlipayApiException e) {
                e.printStackTrace();
            }
            servletResponse.setContentType( "text/html;charset="  + "UTF-8");
            try {
                servletResponse.getWriter().write(form); //直接将完整的表单html输出到页面
                servletResponse.getWriter().flush();
                servletResponse.getWriter().close();
            } catch (IOException e) {
                throw new RuntimeException(e);
            }
        } else {
            System.out.println("调用失败");
        }
    }

//bizContent.put("time_expire", "2022-08-01 22:00:00");

//// 商品明细信息，按需传入
//JSONArray goodsDetail = new JSONArray();
//JSONObject goods1 = new JSONObject();
//goods1.put("goods_id", "goodsNo1");
//goods1.put("goods_name", "子商品1");
//goods1.put("quantity", 1);
//goods1.put("price", 0.01);
//goodsDetail.add(goods1);
//bizContent.put("goods_detail", goodsDetail);

//// 扩展信息，按需传入
//JSONObject extendParams = new JSONObject();
//extendParams.put("sys_service_provider_id", "2088511833207846");
//bizContent.put("extend_params", extendParams);
}
